<?php

namespace app\admin\controller;

use app\common\model\Menu as MenuModel;
use app\common\model\UserType as UserTypeModel;

/**
 * 菜单控制器
 * Class Index
 * @package app\admin\controller
 */
class Menu extends Base
{
    /**
     * [index  菜单列表]
     * @author [C.]
     * @return json
     */
    public function getIndex()
    {
        $model = new MenuModel();
        $user = $this->user;
        $menu = $model->getAllMenu();
        $arr = $this->rule($menu);
        writelog('查看菜单列表',1,$user);
        return admin_json(1,'成功',$arr);
    }

    /**
     * [rule  菜单整理]
     * @author [C。]
     * @param $cate
     * @param string $lefthtml
     * @param int $pid
     * @param int $lvl
     * @param int $leftpin
     * @return json
     */
    public function rule($cate , $lefthtml = '— — ' , $pid=0 , $lvl=0, $leftpin=0 ){
        $arr=array();
        foreach ($cate as $v){
            if($v['pid']==$pid){
                $v['lvl']=$lvl + 1;
                $v['leftpin']=$leftpin + 0;//左边距
                $v['lefthtml']=str_repeat($lefthtml,$lvl);
                $arr[]=$v;
                $arr= array_merge($arr,self::rule($cate,$lefthtml,$v['id'],$lvl+1 , $leftpin+2));
            }
        }
        return $arr;
    }

    /**
     * [add  添加菜单]
     * @author [C。]
     * @return array
     */
    public function postAdd()
    {
        $model = new MenuModel();
        $user = $this->user;
        try{
            $param = input('post.');
            $id = $model->addData($param);
        }catch(\Exception $e){
            return admin_json(-1,$e->getMessage());
        }
        writelog('增加菜单--'.$id,1,$user);
        return admin_json(1,'增加菜单成功');
    }

    /**
     * [edit  编辑菜单]
     * @author [C。]
     * @return array|mixed
     */
    public function postEdit()
    {
        $model = new MenuModel();
        $action = input('param.doedit');
        $user = $this->user;
        if(!empty($action))
        {
            $param = input('post.');
            try {
                $id = $model->editData($param);
            }catch (\Exception $e) {
                return admin_json(-1, '菜单修改失败！');
            }
            writelog('修改菜单--'.$id,1,$user);
            return admin_json(1, '菜单修改成功！');
        }

        $id = input('param.id');

        writelog('查看菜单--'.$id,1,$user);
        return admin_json(1,'成功',$model->findData($id));
    }

    /**
     * [roleDel 删除菜单]
     * @return [type] [description]
     * @author [C。]
     */
    public function getDel()
    {
        $model = new MenuModel();
        $user = $this->user;
        $id = input('param.id');
        $res = $model->deleteTrueData( $id);
        writelog('删除菜单--'.$id,1,$user);

        return admin_json(1,'删除菜单成功');
    }

    /**
     * [status  菜单状态]
     * @author [C。]
     * @return array
     */
    public function status()
    {
        $model = new MenuModel();

        $data=input('param.');
        $res = $model->changeStatus(['id' => $data['id']],$data['status']);
        $user = $this->user;
        if($res)
        {
            if($data['status'] == 0)
            {
                writelog('禁用菜单状态--'.$data['id'],1,$user);
                return admin_json(2,'已禁止菜单');
            }
            else
            {
                writelog('启用菜单状态--'.$data['id'],1,$user);
                return admin_json(1,'已启用菜单');
            }
        }

        return admin_json(-1,'操作失败');
    }
    /**
     * [getMenu  获取菜单]
     * @author [C。]
     * @return array
     */
    public function getMenu()
    {
        $model = new MenuModel;
        $rmodel = new UserTypeModel;
        $user = $this->user;
        $role = $rmodel->findData($user['groupid'],'id','rules');
        $result = $model->getAllData(['pid'=>0],'sort','id,title,pid');

        if(!empty($role)){
            $rule = explode(',', $role['rules']);
        }

        foreach($result as $key=>$vo) {
            if (empty($rule) && in_array($vo['id'], $rule)) {
//                unset($result[$key]);
            }

            $result2 = $model->getAllData(['pid'=>$vo['id']],'sort','id,title,pid');
            $result[$key]['lv'] = $result2;
            if($result2){
                foreach($result2 as $k=>$v){
                    if(!empty($rule) && in_array($v['id'], $rule)){
                        $result[$key]['lv'][$k]['checked'] = true;
                    }else{
                        $result[$key]['lv'][$k]['checked'] = false;
                    }
                    $result3 = $model->getAllData(['pid'=>$v['id']],'sort','id,title,pid');
                    $result[$key]['lv'][$k]['lv'] = $result3;
                    if($result3) {
                        foreach ($result3 as $k1 => $v1) {
                            if(!empty($rule) && in_array($v1['id'], $rule)){
                                $result[$key]['lv'][$k]['lv'][$k1]['checked'] = true;
                            }else{
                                $result[$key]['lv'][$k]['lv'][$k1]['checked'] = false;
                            }

                        }
                    }
                }
            }
        }
        return admin_json(-1,'操作失败',$result);
    }
}